package com.example.backblog.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.google.common.util.concurrent.RateLimiter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
    String[] swaggerExcludes=new String[]{"/swagger-ui.html","/swagger-resources/**","/webjars/**"};
    String[] picutureSubFix =
            new String[]{"/**.xbm","/**.tif","/**.pjp","/**.svgz","/**.jpg","/**.jpeg","/**.ico","/**.tiff","/**..gif",
                    "/**.svg","/**.jfif","/**.webp","/**.png","/**.bmp","/**.pjpeg","/**.avif","/**.mp3"};
    @Autowired
    private LoginInterceptor loginInterceptor;
    @Autowired
    private RateLimitInterceptor rateLimitInterceptor;
    @Autowired
    private UserIdHandlerMethodArgumentResolver userIdHandlerMethodArgumentResolver;

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        WebMvcConfigurer.super.addArgumentResolvers(resolvers);
        resolvers.add(userIdHandlerMethodArgumentResolver);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        List<String> collect = Arrays.stream(picutureSubFix).map((pic) -> pic.toUpperCase()).collect(Collectors.toList());
//
//        registry.addInterceptor(rateLimitInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns(swaggerExcludes)
//                .excludePathPatterns(picutureSubFix)
//                .excludePathPatterns(collect);
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/user/**")
                .addPathPatterns("/file/**")
                .excludePathPatterns("/user/login/**");

    }
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
//        configurer.addPathPrefix("/api", c -> c.isAnnotationPresent(RestController.class ) || c.isAnnotationPresent(Controller.class));
    }

//    /**
//     * SpringBoot 2.x要重写该方法，
//     * @param registry
//     */
////    @Override
////    public void addResourceHandlers(ResourceHandlerRegistry registry) {
////        registry.addResourceHandler("/**")
////                .addResourceLocations("classpath:/META-INF/resources/")
////                .addResourceLocations("classpath:/META-INF/resources/image/")
////                .addResourceLocations("classpath:/resources/")
////                .addResourceLocations("classpath:/resources/upload/")
////                .addResourceLocations("classpath:/upload/");
////    }
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**");
            }

        };
    }
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
        return factory -> {
            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/index.html");
            factory.addErrorPages(error404Page);
        };
    }
}
